# -*- coding: utf-8 -*-

# @Time : 2022/6/15 15:00

# @Author : scy

# @File : __init__.py.py

# @Software: PyCharm

import tkinter
import tkinter as tk
import tkinter.messagebox
import tkinter.ttk as ttk
from tkinter import *

from PIL import ImageTk, Image

import interact.user


# db = pymysql.connect(host='localhost',user='root',password='123456',database='teachindex',charset='utf8')

def users():
    user = interact.user.user()
    # 第1步，实例化object，建立窗口window
    window = tk.Tk()

    # 第2步，给窗口的可视化起名字
    window.title('用户登录')

    # 第3步，设定窗口的大小(长 * 宽)
    window.geometry('400x400')  # 这里的乘是小x

    def get_img(filename, width, height):
        im = Image.open(filename).resize((width, height))
        im = ImageTk.PhotoImage(im)
        return im

    im_root = get_img('1.gif', width=1600, height=900)
    canvas_root = tk.Canvas(window, width=1600, height=900)
    canvas_root.create_image(800, 450, image=im_root)
    canvas_root.pack()

    # 第4步，加载 wellcome image
    # canvas = tk.Canvas(window, width=400, height=135, bg='green')
    # image_file = tk.PhotoImage(file='pic.gif')
    # image = canvas.create_image(200, 0, anchor='n', image=image_file)
    # canvas.pack(side='top')
    # tk.Label(window, text='Wellcome', font=('Arial', 16)).pack()

    # 第5步，用户信息
    tk.Label(window, text='用户名:', font=('Arial', 14)).place(x=10, y=170)
    tk.Label(window, text='密码:', font=('Arial', 14)).place(x=10, y=210)

    # 第6步，用户登录输入框entry
    # 用户名
    var_usr_name = tk.StringVar()
    entry_usr_name = tk.Entry(window, textvariable=var_usr_name, font=('Arial', 14))
    entry_usr_name.place(x=120, y=175)
    # 用户密码
    var_usr_pwd = tk.StringVar()
    entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, font=('Arial', 14), show='*')
    entry_usr_pwd.place(x=120, y=215)

    # 第8步，定义用户登录功能
    def usr_login():
        # 这两行代码就是获取用户输入的usr_name和usr_pwd
        usr_name = var_usr_name.get()
        usr_pwd = var_usr_pwd.get()

        login = user.login(usr_name, usr_pwd)
        print(login)
        if login == '登陆成功':
            window.destroy()
            # 定义主页窗口
            window_home_page = tk.Tk()
            window_home_page.geometry('1700x800')
            window_home_page.title('主页')

            def get_img_1(filename, width='2000', height='800'):
                im = Image.open(filename).resize((width, height))
                im = ImageTk.PhotoImage(im)
                return im

            im_root = get_img_1('1.gif', width=1600, height=900)
            canvas_root = tk.Canvas(window_home_page, width=1600, height=900)
            canvas_root.create_image(800, 450, image=im_root)
            canvas_root.pack()

            tab_main = ttk.Notebook(window_home_page)  # 创建分页栏
            tab_main.place(relx=0.02, rely=0.02, relwidth=0.95, relheight=0.95)

            tab1 = Frame(tab_main)  # 创建第一页框架
            tab1.place(x=0, y=30)
            tab_main.add(tab1, text='可能心动的人')  # 将第一页插入分页栏中

            def get_img_2(filename, width, height):
                im = Image.open(filename).resize((width, height))
                im = ImageTk.PhotoImage(im)
                return im

            im_root = get_img_2('background.gif', width=1600, height=900)
            canvas_root = tk.Canvas(tab1, width=1600, height=900)
            canvas_root.create_image(800, 450, image=im_root)
            canvas_root.pack()

            # 调用随机生成相亲对象函数
            information = user.random_love()
            # print(information)
            n1 = 10
            n2 = 10
            for result in information:
                for res in result:
                    if res != '' and res != None:
                        print(res, end=' ')
                        tk.Label(tab1, text=res, font=('Arial', 12)).place(x=n1, y=n2)
                        n1 = n1 + 130
                n1 = 10
                n2 = n2 + 70
                print()
            # i=0
            # len_information=len(information)
            # print(len_information)
            # n = 10
            # n1 = 10
            # while i<len_information:
            #     tk.Label(tab1, text=information[i], font=('Arial', 14)).place(x=n, y=n1)
            #     n1=n1+100
            #     i=i+1

            tab2 = Frame(tab_main)
            tab2.place(x=100, y=30)
            tab_main.add(tab2, text='修改信息')

            new_cname = tk.StringVar()
            tk.Label(tab2, text='姓名: ').place(x=10, y=50)
            entry_new_cname = tk.Entry(tab2, textvariable=new_cname)
            entry_new_cname.place(x=130, y=50)
            # 性别
            tk.Label(tab2, text='性别: ').place(x=10, y=90)
            # 下拉框
            new_csex = ttk.Combobox(tab2, width=12, textvariable=tkinter.StringVar(), state="readonly")
            lis = ['男', '女']
            new_csex['values'] = lis
            new_csex.place(x=130, y=90)
            # 身高
            new_cheight = tk.StringVar()
            tk.Label(tab2, text='身高: ').place(x=10, y=130)
            entry_new_cheight = tk.Entry(tab2, textvariable=new_cheight)
            entry_new_cheight.place(x=130, y=130)
            # 体重
            new_cweight = tk.StringVar()
            tk.Label(tab2, text='体重: ').place(x=10, y=170)
            entry_new_cweight = tk.Entry(tab2, textvariable=new_cweight)
            entry_new_cweight.place(x=130, y=170)
            # 学历
            tk.Label(tab2, text='学历: ').place(x=10, y=210)
            # 下拉框
            new_ceducation = ttk.Combobox(tab2, width=12, textvariable=tkinter.StringVar(), state="readonly")
            lis = ['研究生', '本科', '专科', '高中', '初中', '小学']
            new_ceducation['values'] = lis
            new_ceducation.place(x=130, y=210)
            # 要求
            new_cdemand = tk.StringVar()
            tk.Label(tab2, text='要求: ').place(x=10, y=250)
            entry_new_cdemand = tk.Entry(tab2, textvariable=new_cdemand)
            entry_new_cdemand.place(x=130, y=250)
            # 照片
            # 生日
            new_cage_of_birth = tk.StringVar()
            tk.Label(tab2, text='生日: ').place(x=10, y=290)
            entry_new_cage_of_birth = tk.Entry(tab2, textvariable=new_cage_of_birth)
            entry_new_cage_of_birth.place(x=130, y=290)
            # 爱好
            new_chobby = tk.StringVar()
            tk.Label(tab2, text='爱好: ').place(x=10, y=330)
            entry_new_chobby = tk.Entry(tab2, textvariable=new_chobby)
            entry_new_chobby.place(x=130, y=330)
            # 状态
            tk.Label(tab2, text='状态: ').place(x=10, y=370)
            # 下拉框
            new_cown_state = ttk.Combobox(tab2, width=12, textvariable=tkinter.StringVar(), state="readonly")
            lis = ['单身', '约会中', '牵手成功']
            new_cown_state['values'] = lis
            new_cown_state.place(x=130, y=370)
            # 好感人
            new_cgood_impression1 = tk.StringVar()
            tk.Label(tab2, text='好感人1: ').place(x=10, y=410)
            entry_new_cgood_impression1 = tk.Entry(tab2, textvariable=new_cgood_impression1)
            entry_new_cgood_impression1.place(x=130, y=410)
            # 好感人
            new_cgood_impression2 = tk.StringVar()
            tk.Label(tab2, text='好感人2: ').place(x=10, y=440)
            entry_new_cgood_impression2 = tk.Entry(tab2, textvariable=new_cgood_impression2)
            entry_new_cgood_impression2.place(x=130, y=440)
            # 好感人
            new_cgood_impression3 = tk.StringVar()
            tk.Label(tab2, text='好感人3: ').place(x=10, y=470)
            entry_new_cgood_impression3 = tk.Entry(tab2, textvariable=new_cgood_impression3)
            entry_new_cgood_impression3.place(x=130, y=470)
            # 约会
            new_dating = tk.StringVar()
            tk.Label(tab2, text='约会: ').place(x=10, y=510)
            entry_new_dating = tk.Entry(tab2, textvariable=new_dating)
            entry_new_dating.place(x=130, y=510)

            def change_get():
                # 以下就是获取我们注册时所输入的信息
                cname = new_cname.get()
                cheight = new_cheight.get()
                cweight = new_cweight.get()
                ceducation = new_ceducation.get()
                cdemand = new_cdemand.get()
                cage_of_birth = new_cage_of_birth.get()
                chobby = new_chobby.get()
                csex = new_csex.get()
                cphoto = 'a'
                cown_state = new_cown_state
                cgood_impression1 = new_cgood_impression1
                cgood_impression2 = new_cgood_impression2
                cgood_impression3 = new_cgood_impression3
                cdating = new_dating
                change1 = user.modify_information(cname, csex, cheight, cweight, ceducation, chobby,
                                                  cdemand, cown_state,
                                                  cgood_impression1, cgood_impression2, cgood_impression3, cdating,
                                                  cphoto,
                                                  cage_of_birth)
                if change1 == '修改成功':
                    tkinter.messagebox.showinfo(title='成功', message='修改成功')
                elif change1 == "错误，请联系工作人员":
                    tkinter.messagebox.showinfo(title='成功', message='信息已修改')

            btn_change = tk.Button(tab2, text='确认修改', command=change_get)
            btn_change.place(x=50, y=550)

            tab3 = Frame(tab_main)
            tab3.place(x=100, y=30)
            tab_main.add(tab3, text='修改密码')

            # 原密码
            old_password = tk.StringVar()
            tk.Label(tab3, text='原密码: ').place(x=10, y=10)
            entry_old_password = tk.Entry(tab3, textvariable=old_password)
            entry_old_password.place(x=130, y=10)
            # 新密码
            new_password_modify = tk.StringVar()
            tk.Label(tab3, text='新密码: ').place(x=10, y=50)
            entry_new_password_modify = tk.Entry(tab3, textvariable=new_password_modify)
            entry_new_password_modify.place(x=130, y=50)
            # 确定新密码
            repeat_password_modify = tk.StringVar()
            tk.Label(tab3, text='再次输入: ').place(x=10, y=90)
            entry_repeat_password_modify = tk.Entry(tab3, textvariable=repeat_password_modify)
            entry_repeat_password_modify.place(x=130, y=90)

            def modify():
                old_password_1 = old_password
                new_password = new_password_modify
                repeat_password = repeat_password_modify
                modify_1 = user.modify_password(old_password_1, new_password, repeat_password)
                if modify_1 == "原密码错误":
                    tkinter.messagebox.showinfo(title='提示', message='修改成功')
                elif modify_1 == '两次密码不相同，请重新输入':
                    tkinter.messagebox.showinfo(title='错误', message='两次密码不相同，请重新输入')
                elif modify_1 == '密码长度不符合要求':
                    tkinter.messagebox.showinfo(title='错误', message='密码长度不符合要求')
                elif modify_1 == '密码不符合要求，密码必须是字母或者是数字':
                    tkinter.messagebox.showinfo(title='错误', message='密码不符合要求，密码必须是字母或者是数字')
                elif modify_1 == '密码修改成功':
                    tkinter.messagebox.showinfo(title='成功', message='密码修改成功')
                elif modify_1 == '错误，请联系工作人员':
                    tkinter.messagebox.showinfo(title='错误', message='错误，请联系工作人员')

            btn_change = tk.Button(tab3, text='确认修改', command=modify)
            btn_change.place(x=50, y=130)

            tab4 = Frame(tab_main)
            tab4.place(x=100, y=30)
            tab_main.add(tab4, text='添加好感人')
            # 好感人
            good_phone_number_1 = tk.StringVar()
            tk.Label(tab4, text='添加好感人: ').place(x=10, y=50)
            entry_good_phone_number = tk.Entry(tab4, textvariable=good_phone_number_1)
            entry_good_phone_number.place(x=130, y=50)

            def good_add():
                good_phone_number = good_phone_number_1
                good_phone_number_add = user.add_good_impression(good_phone_number)
                if good_phone_number_add == '都已经牵手成功了，做个人吧':
                    tkinter.messagebox.showinfo(title='错误', message='都已经牵手成功了，做个人吧')
                elif good_phone_number_add == '已有约会人，请不要做渣男':
                    tkinter.messagebox.showinfo(title='提示', message='添加成功')
                elif good_phone_number_add == '已有三个好感人，无法添加，如果还需添加，请删除几个好感人':
                    tkinter.messagebox.showinfo(title='错误', message='已有三个好感人，无法添加，如果还需添加，请删除几个好感人')
                elif good_phone_number_add == '好感人加入成功':
                    tkinter.messagebox.showinfo(title='成功', message='好感人加入成功')
                elif good_phone_number_add == '错误，请联系工作人员':
                    tkinter.messagebox.showinfo(title='错误', message='错误，请联系工作人员')

            btn_change = tk.Button(tab4, text='确认添加', command=good_add)
            btn_change.place(x=50, y=130)

            # 请求约会
            tab5 = Frame(tab_main)
            tab5.place(x=100, y=30)
            tab_main.add(tab5, text='请求约会')
            dating_phone_number_1 = tk.StringVar()
            tk.Label(tab5, text='请求约会: ').place(x=10, y=50)
            entry_dating_phone_number = tk.Entry(tab5, textvariable=dating_phone_number_1)
            entry_dating_phone_number.place(x=130, y=50)

            def dating_people():
                dating_phone_number = dating_phone_number_1
                dating = user.ask_dating(dating_phone_number)
                if dating == '都已经牵手成功了，做个人吧':
                    tkinter.messagebox.showinfo(title='错误', message='都已经牵手成功了，做个人吧')
                elif dating == "已有约会人，无法添加":
                    tkinter.messagebox.showinfo(title='提示', message='添加成功')
                elif dating == '请求约会人不在好感人列表':
                    tkinter.messagebox.showinfo(title='错误', message='请求约会人不在好感人列表')
                elif dating == '用户不存在':
                    tkinter.messagebox.showinfo(title='错误', message='用户不存在')
                elif dating == '很遗憾，对方对你没有好感':
                    tkinter.messagebox.showinfo(title='错误', message='很遗憾，对方对你没有好感')
                elif dating == '可以约会,请你选择约会地点，时间等信息':
                    tkinter.messagebox.showinfo(title='成功', message='可以约会,请你选择约会地点，时间等信息')
                elif dating == '错误，请联系工作人员':
                    tkinter.messagebox.showinfo(title='错误', message='错误，请联系工作人员')

            btn_change = tk.Button(tab5, text='确认添加', command=dating_people)
            btn_change.place(x=50, y=130)

            # 添加记录
            tab6 = Frame(tab_main)
            tab6.place(x=100, y=30)
            tab_main.add(tab6, text='添加约会信息')

            dating_phone_number_1 = tk.StringVar()
            tk.Label(tab6, text='添加用户: ').place(x=10, y=50)
            entry_dating_phone_number = tk.Entry(tab6, textvariable=dating_phone_number_1)
            entry_dating_phone_number.place(x=130, y=50)
            dating_time_1 = tk.StringVar()
            tk.Label(tab6, text='添加时间: ').place(x=10, y=90)
            entry_dating_phone_number = tk.Entry(tab6, textvariable=dating_time_1)
            entry_dating_phone_number.place(x=130, y=90)
            place_1 = tk.StringVar()
            tk.Label(tab6, text='添加地点: ').place(x=10, y=130)
            entry_dating_phone_number = tk.Entry(tab6, textvariable=place_1)
            entry_dating_phone_number.place(x=130, y=130)

            def add_record_1():
                dating_phone_number = dating_phone_number_1
                dating_time = dating_time_1
                place = place_1
                add = user.add_record(dating_phone_number, dating_time, place)
                if add == '该地点人数过多，请重新选择地点':
                    tkinter.messagebox.showinfo(title='错误', message='该地点人数过多，请重新选择地点')
                elif add == '约会地点增加成功，记得评论满意程度':
                    tkinter.messagebox.showinfo(title='成功', message='约会地点增加成功，记得评论满意程度')
                elif add == '错误，请联系工作人员':
                    tkinter.messagebox.showinfo(title='提示', message='添加成功')

            btn_change = tk.Button(tab6, text='确认添加', command=add_record_1)
            btn_change.place(x=50, y=170)

            # 读取约会地点
            tab7 = Frame(tab_main)
            tab7.place(x=100, y=30)
            tab_main.add(tab7, text='读取约会地点')
            information_1 = user.all_dating_recors()
            print(information_1)
            i = 0
            len_information_1 = len(information_1)
            print(len_information_1)
            n = 10
            n1 = 10
            while i < len_information_1:
                tk.Label(tab7, text=information_1[i], font=('Arial', 14)).place(x=n, y=n1)
                n1 = n1 + 100
                i = i + 1

            # 对约会人进行评分
            tab8 = Frame(tab_main)
            tab8.place(x=100, y=30)
            tab_main.add(tab8, text='对约会人评价')

            dating_phone_daing_1 = tk.StringVar()
            tk.Label(tab8, text='对象用户: ').place(x=10, y=50)
            entry_dating_phone_daing = tk.Entry(tab8, textvariable=dating_phone_daing_1)
            entry_dating_phone_daing.place(x=130, y=50)
            satisfaction_1 = tk.StringVar()
            tk.Label(tab8, text='输入评价: ').place(x=10, y=90)
            entry_satisfaction = tk.Entry(tab8, textvariable=satisfaction_1)
            entry_satisfaction.place(x=130, y=90)

            def score_1():
                dating_phone_daing = dating_phone_daing_1
                satisfaction = satisfaction_1
                score_2 = user.score(dating_phone_daing, satisfaction)
                if score_2 == '恭喜你，牵手成功':
                    tkinter.messagebox.showinfo(title='恭喜', message='恭喜你，牵手成功')
                elif score_2 == '很遗憾，牵手失败':
                    tkinter.messagebox.showinfo(title='抱歉', message='很遗憾，牵手失败')
                elif score_2 == '评价成功':
                    tkinter.messagebox.showinfo(title='提示', message='评价成功')
                elif score_2 == '错误，请联系工作人员':
                    tkinter.messagebox.showinfo(title='提示', message='评价成功')

            btn_change = tk.Button(tab8, text='确认提交', command=score_1)
            btn_change.place(x=50, y=170)

            # 查询对自己满意人的信息
            tab9 = Frame(tab_main)
            tab9.place(x=100, y=30)
            tab_main.add(tab9, text='查询对自己满意人的信息')
            information_2 = user.inquire_loev_me()
            print(information_2)
            i = 0
            len_information_2 = len(information_2)
            print(len_information_2)
            n = 10
            n1 = 10
            while i < len_information_2:
                tk.Label(tab9, text=information_2[i], font=('Arial', 14)).place(x=n, y=n1)
                n1 = n1 + 100
                i = i + 1

        elif login == '用户不存在':
            tkinter.messagebox.showinfo(title='错误', message='用户不存在')
        elif login == '密码错误，请重新输入':
            tkinter.messagebox.showinfo(title='错误', message='密码错误，请重新输入')

    # 第9步，定义用户注册功能
    def usr_sign_up():
        user = interact.user.user()
        # 定义长在窗口上的窗口
        window_sign_up = tk.Toplevel(window)
        window_sign_up.geometry('600x900')
        window_sign_up.title('注册界面')
        # 用户名（手机号）
        new_cphone_number = tk.StringVar()  # 将输入的注册名赋值给变量
        tk.Label(window_sign_up, text='用户名: ').place(x=10, y=10)  # 将`User name:`放置在坐标（10,10）。
        entry_cphone_number = tk.Entry(window_sign_up,
                                       textvariable=new_cphone_number)  # 创建一个注册名的`entry`，变量为`cphone_number`
        entry_cphone_number.place(x=130, y=10)  # `entry`放置在坐标（150,10）.
        # 姓名
        new_cname = tk.StringVar()
        tk.Label(window_sign_up, text='姓名: ').place(x=10, y=50)
        entry_new_cname = tk.Entry(window_sign_up, textvariable=new_cname)
        entry_new_cname.place(x=130, y=50)
        # 性别
        tk.Label(window_sign_up, text='性别: ').place(x=10, y=90)
        # 下拉框
        new_csex = ttk.Combobox(window_sign_up, width=12, textvariable=tkinter.StringVar(), state="readonly")
        lis = ['男', '女']
        new_csex['values'] = lis
        new_csex.place(x=130, y=90)
        # 密码
        new_cpassword = tk.StringVar()
        tk.Label(window_sign_up, text='密码: ').place(x=10, y=130)
        entry_new_cpassword = tk.Entry(window_sign_up, textvariable=new_cpassword, show='*')
        entry_new_cpassword.place(x=130, y=130)
        # 确认密码
        new_repeat_password = tk.StringVar()
        tk.Label(window_sign_up, text='确认密码: ').place(x=10, y=170)
        entry_new_repeat_password = tk.Entry(window_sign_up, textvariable=new_repeat_password, show='*')
        entry_new_repeat_password.place(x=130, y=170)
        # 身高
        new_cheight = tk.StringVar()
        tk.Label(window_sign_up, text='身高: ').place(x=10, y=210)
        entry_new_cheight = tk.Entry(window_sign_up, textvariable=new_cheight)
        entry_new_cheight.place(x=130, y=210)
        # 体重
        new_cweight = tk.StringVar()
        tk.Label(window_sign_up, text='体重: ').place(x=10, y=250)
        entry_new_cweight = tk.Entry(window_sign_up, textvariable=new_cweight)
        entry_new_cweight.place(x=130, y=250)
        # 学历
        tk.Label(window_sign_up, text='学历: ').place(x=10, y=290)
        # 下拉框
        new_ceducation = ttk.Combobox(window_sign_up, width=12, textvariable=tkinter.StringVar(), state="readonly")
        lis = ['研究生', '本科', '专科', '高中', '初中', '小学']
        new_ceducation['values'] = lis
        new_ceducation.place(x=130, y=290)
        # 要求
        new_cdemand = tk.StringVar()
        tk.Label(window_sign_up, text='要求: ').place(x=10, y=330)
        entry_new_cdemand = tk.Entry(window_sign_up, textvariable=new_cdemand)
        entry_new_cdemand.place(x=130, y=330)
        # 照片
        # 生日
        new_cage_of_birth = tk.StringVar()
        tk.Label(window_sign_up, text='生日: ').place(x=10, y=370)
        entry_new_cage_of_birth = tk.Entry(window_sign_up, textvariable=new_cage_of_birth)
        entry_new_cage_of_birth.place(x=130, y=370)
        # 爱好
        new_chobby = tk.StringVar()
        tk.Label(window_sign_up, text='爱好: ').place(x=10, y=410)
        entry_new_chobby = tk.Entry(window_sign_up, textvariable=new_chobby)
        entry_new_chobby.place(x=130, y=410)

        def sign_to_hongwei_website():
            # 以下就是获取我们注册时所输入的信息
            cpassword = new_cpassword.get()
            repeat_password = new_repeat_password.get()
            cphone_number = new_cphone_number.get()
            cname = new_cname.get()
            cheight = new_cheight.get()
            cweight = new_cweight.get()
            ceducation = new_ceducation.get()
            cdemand = new_cdemand.get()
            cage_of_birth = new_cage_of_birth.get()
            chobby = new_chobby.get()
            csex = new_csex.get()
            cphoto = 'a'

            sign_up = user.register(cphone_number, cname, csex, cpassword, repeat_password, cheight, cweight,
                                    ceducation, chobby,
                                    cdemand, cphoto, cage_of_birth)
            if sign_up == '创建成功，即将跳转到登陆成功界面':
                tkinter.messagebox.showinfo(title='恭喜', message='恭喜你，创建成功')

                # 然后销毁窗口。
                window_sign_up.destroy()

        # 下面的 sign_to_hongwei_website
        btn_comfirm_sign_up = tk.Button(window_sign_up, text='确认注册', command=sign_to_hongwei_website)
        btn_comfirm_sign_up.place(x=150, y=440)

    # 第7步，login（登录） and sign up（注册） 按钮
    btn_login = tk.Button(window, text='登录', command=usr_login)
    btn_login.place(x=120, y=300)
    btn_sign_up = tk.Button(window, text='注册', command=usr_sign_up)
    btn_sign_up.place(x=300, y=300)

    # 第10步，主窗口循环显示
    window.mainloop()


if __name__ == '__main__':
    users()
